package com.example.mvvmtest.view

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.mvvmtest.R
import com.example.mvvmtest.databinding.ActivityRegisterBinding
import com.example.mvvmtest.model.database.UserDatabase
import com.example.mvvmtest.Repository.UserRepository
import com.example.mvvmtest.viewModel.RegisterActivityViewModel

class RegisterActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRegisterBinding
    private lateinit var viewModel: RegisterActivityViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // 初始化数据绑定
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register)

        // 初始化ViewModel
        val dao = UserDatabase.getDatabase(application).userDao()
        val repository = UserRepository(dao)
        viewModel = ViewModelProvider(this, object : ViewModelProvider.Factory {
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                if (modelClass.isAssignableFrom(RegisterActivityViewModel::class.java)) {
                    return RegisterActivityViewModel(repository) as T
                }
                throw IllegalArgumentException("Unknown ViewModel class")
            }
        })[RegisterActivityViewModel::class.java]
        
        // 绑定ViewModel
        binding.viewModel = viewModel
        binding.lifecycleOwner = this

        // 手动处理文本输入变化
        binding.account.addTextChangedListener(object : android.text.TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: android.text.Editable?) {
                if (s != null) {
                    viewModel.onIdChanged(s.toString())
                }
            }
        })

        binding.password.addTextChangedListener(object : android.text.TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: android.text.Editable?) {
                if (s != null) {
                    viewModel.onPasswordChanged(s.toString())
                }
            }
        })

        binding.confirmPassword.addTextChangedListener(object : android.text.TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: android.text.Editable?) {
                if (s != null) {
                    viewModel.onConfirmPasswordChanged(s.toString())
                }
            }
        })

        // 注册按钮点击事件
        binding.registerButton.setOnClickListener {
            viewModel.register()
        }

        // 跳转到登录页面
        binding.loginText.setOnClickListener {
            val intent = Intent(this, LoginActivity::class.java)
            startActivity(intent)
            finish()
        }

        // 注册成功返回登录页
        viewModel.registerSuccess.observe(this) {
            if (it) {
                val intent = Intent(this, LoginActivity::class.java)
                startActivity(intent)
                finish()
            }
        }
    }
}